package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 自动填充切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {}

    /**
     * 自动填充切面操作，给字段赋值
     */
    @Before("autoFillPointCut()")
    public void autoFillAspect(JoinPoint joinPoint) {
        log.info("开始自动填充字段");
        //获取方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获得方法上的注解
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
        //通过joinPoint获取传入方法的参数
        Object object = joinPoint.getArgs()[0];
        //声明变量time和userId并通过BaseContext给其赋值
        Long userId = BaseContext.getCurrentId();
        LocalDateTime time = LocalDateTime.now();
        //判断注解类型
        if (annotation.value() == OperationType.INSERT) {
            try {
                //1. 如果为insert，则修改createTime，updateTime，createUser，updateUser
                //获取方法对象
                Method setCreateTime = object.getClass().getMethod("setCreateTime", LocalDateTime.class);
                Method setUpdateTime = object.getClass().getMethod("setUpdateTime", LocalDateTime.class);
                Method setCreateUser = object.getClass().getMethod("setCreateUser", Long.class);
                Method setUpdateUser = object.getClass().getMethod("setUpdateUser", Long.class);

                //调用方法，传入参数和对象
                setCreateTime.invoke(object, time);
                setUpdateTime.invoke(object, time);
                setCreateUser.invoke(object, userId);
                setUpdateUser.invoke(object, userId);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } else if (annotation.value() == OperationType.UPDATE) {
            try {
                //2. 如果为update，则修改updateTime，updateUser
                //获取方法对象
                Method setUpdateTime = object.getClass().getMethod("setUpdateTime", LocalDateTime.class);
                Method setUpdateUser = object.getClass().getMethod("setUpdateUser", Long.class);

                //调用方法，传入参数和对象
                setUpdateTime.invoke(object, time);
                setUpdateUser.invoke(object, userId);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }














}